package com.block.job.service;

import cn.hutool.core.util.StrUtil;
import com.block.common.service.LogFactory;
import com.block.db.mapper.MerchantDataPushInfoMapper;
import com.block.db.mapper.MerchantThirdBaseInfoMapper;
import com.block.db.mapper.MerchantThirdProtocolInfoMapper;
import com.block.db.mapper.SourceMemberDataInfoHistroyMapper;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.slf4j.Logger;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDate;

@Component
public class DataClearJob {

    private static final Logger log = LogFactory.getJobLog();

    @Resource
    MerchantThirdBaseInfoMapper merchantThirdBaseInfoMapper;
    @Resource
    MerchantThirdProtocolInfoMapper merchantThirdProtocolInfoMapper;
    @Resource
    SourceMemberDataInfoHistroyMapper sourceMemberDataInfoHistroyMapper;

    /**
     * 数据清理 每日凌晨一点半定时执行
     * @throws Exception
     */
    @XxlJob("dataClearJob")
    public void dataClearJob() throws Exception {
        XxlJobHelper.log("数据清理---------开始执行--------");

        // 删除7天前 商户的机构相关数据
        String dateInfo = LocalDate.now().minusDays(7).toString();
        merchantThirdBaseInfoMapper.deleteDataByDateInfo(dateInfo);
        merchantThirdBaseInfoMapper.optimizeTable();

        merchantThirdProtocolInfoMapper.deleteDataByDateInfo(dateInfo);
        merchantThirdProtocolInfoMapper.optimizeTable();

        // 删除7天前的线索撞库历史
        String dateInfo1 = LocalDate.now().minusDays(7).toString();
        sourceMemberDataInfoHistroyMapper.deleteDataByDateInfo(dateInfo1);
        sourceMemberDataInfoHistroyMapper.optimizeTable();

        XxlJobHelper.log("数据清理 ---------结束执行--------");
    }

    /**
     * ALTER TABLE `fenqi-service`.`lq_merchant_third_base_info`
     * ADD COLUMN `date_info` varchar(20) NULL AFTER `id`;
     *
     * ALTER TABLE `fenqi-service`.`lq_merchant_third_protocol_info`
     * ADD COLUMN `date_info` varchar(20) NULL AFTER `id`;
     */

}
